package com.itheima.controller.pms;

import com.itheima.api.pms.PmsProductAttributeCategoryService;
import com.itheima.vo.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("productAttribute/category")
public class PmsProductAttributeCategoryController {

    @DubboReference
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;


    /**
     * 1.添加商品属性分类
     *
     * @param name
     * @return
     */
    @PostMapping("create")
    public AjaxResult createByName(String name) {
        return pmsProductAttributeCategoryService.createByName(name);
    }

    /**
     * 2.删除单个商品属性分类
     *
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public AjaxResult deleteById(@PathVariable(value = "id", required = true) Long id) {
        return pmsProductAttributeCategoryService.deleteById(id);
    }

    /**
     * 3.分页获取所有商品属性分类
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public AjaxResult findByList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return pmsProductAttributeCategoryService.findByList(pageNum, pageSize);
    }


    /**
     * 4.获取所有商品属性分类及其下属性
     *
     * @return
     */
    @GetMapping("list/withAttr")
    public AjaxResult findByListwithAttr() {
        return pmsProductAttributeCategoryService.findByListwithAttr();
    }

    /**
     * 5.修改商品属性分类
     *
     * @param id
     * @param name
     * @return
     */
    @PostMapping("update/{id}")
    public AjaxResult updateByIdAndName(@PathVariable(value = "id", required = true) Long id,
                                        @RequestParam(value = "name", required = true) String name) {
        return pmsProductAttributeCategoryService.updateByIdAndName(id, name);
    }

    /**
     * 6.获取单个商品属性分类信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AjaxResult findById(@PathVariable(value = "id", required = true) Long id) {
        return pmsProductAttributeCategoryService.findById(id);
    }
}
